import 'dart:io';
import 'package:permission_handler/permission_handler.dart';

const String audioStartName = "record_start";
const String audioSuccessName = "record_success";

///
/// 语音识别权限管理类
///
class AudioController {
  ///
  /// 请求麦克风权限
  ///
  static Future<bool> requestAudioPermissions() async {
    final granted = await hasMicrophonePermission();

    if (granted) {
      if (Platform.isAndroid) {
        PermissionStatus foregroundServicePermission = await Permission.systemAlertWindow.request();
        if (!foregroundServicePermission.isGranted) {
          foregroundServicePermission = await Permission.systemAlertWindow.request();
        }
      }
      return true;
    }
    return false;
  }

  ///
  /// 麦克风权限
  ///
  static Future<bool> hasMicrophonePermission() async {
    final state = await Permission.microphone.request();
    return state == PermissionStatus.granted;
  }

  ///
  /// 语音识别权限
  ///
  static Future<bool> hasSpeechRecognitionPermission() async {
    final state = await Permission.speech.request();
    return state == PermissionStatus.granted;
  }
}

///
/// 语音状态枚举
///
enum VoiceStatus {
  /// 默认状态 未交互/交互完成
  idle,

  /// 录音中
  recording,

  /// 取消发送
  cancelSend,

  /// 语音转文字
  textProcessing;

  String get voiceText {
    switch (this) {
      case VoiceStatus.idle:
        return "按住 说话";
      case VoiceStatus.recording:
        return "松开 发送";
      case VoiceStatus.cancelSend:
        return "松开 取消";
      case VoiceStatus.textProcessing:
        return "语音转文字";
    }
  }
}
